package com.avertymaroc.users;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.avertymaroc.app.R;
import com.avertymaroc.library.C;
import com.avertymaroc.library.HttpManager;
import com.avertymaroc.library.ImageLoader;
import com.avertymaroc.library.SessionManager;

public class Sondage_public extends Activity{
	
	
	private ArrayList<HashMap<String, String>> list_sondage_pub;
	private ListView mListView;
	private MyAdapter mAdapter;
	private ImageButton logOut;
	private ImageButton homePage;
	private static final String KEY_TITRE 		= "titre";
	private static final String KEY_REPONSES 	= "reponses";
	private static final String KEY_IMAGE	 	= "image";
	private static final String KEY_ID		 	= "id";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sondage_public);
		init();
	}

	
	private void init(){
		list_sondage_pub = new ArrayList<HashMap<String,String>>();
		mListView 		 = (ListView) findViewById(R.id.list_sondage_pub);
		
		initActionBarButtons();
		
		mListView.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> adapter, View v, int position,
					long id) {
					participer();
			}
			
		});
		
		HttpManager listSondage = new HttpManager(this) {
			
			@Override
			protected void onPostExecute(String file_url) {
				this.getpDialog().dismiss();
				
				JSONObject res = getResult();
				try {
					JSONArray infos= res.getJSONArray("infos");
					HashMap<String, String> map;
					
					for(int i = 0; i < infos.length() ; i++){
						map = new HashMap<String, String>();
						JSONObject obj = infos.getJSONObject(i);
						map.put(KEY_TITRE, obj.getString("titre" ));
						map.put(KEY_REPONSES, obj.getString("nb" ));
						map.put(KEY_ID,  obj.getString("id" ));
						list_sondage_pub.add(map);
					}
					mAdapter = new MyAdapter();
					
					if(list_sondage_pub.size() == 0){
						TextView t = new TextView(getApplicationContext());
						t.setText("il n'y a pas de sondage actualement. ");
						 mListView.addHeaderView(t);
					}
					
					mListView.setAdapter(mAdapter);
					
				} catch (JSONException e) {
					
					Toast.makeText(getApplicationContext(), e.getMessage(), 3000).show();;
				}
			}
		};
		listSondage.setUrl(C.site_url("mobile/articles/sondage_public_encours"))
				   .execute();
		

	}
	
	class MyAdapter extends BaseAdapter{
		
	
		private  LayoutInflater inflater=null;
		public ImageLoader imageLoader; 
	
		
		public MyAdapter() {
			 inflater = (LayoutInflater)Sondage_public.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			 imageLoader = new ImageLoader(getApplicationContext());
		}

		@Override
		public int getCount() {
			return list_sondage_pub.size();
		}

		@Override
		public Object getItem(int position) {	
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null)
				convertView = inflater.inflate(R.layout.row_sondg_pub_list, null);
			
			HashMap< String, String> map = list_sondage_pub.get(position);
			
			
			TextView titre = (TextView) convertView.findViewById(R.id.titre_article);
			TextView nb_reponse = (TextView) convertView.findViewById(R.id.nb_reponse);
			ImageView image = (ImageView) convertView.findViewById(R.id.image_list);
			titre.setText(map.get(KEY_TITRE));
			nb_reponse.setText(map.get(KEY_REPONSES)+" Votes");
			return convertView;
		}
		
	}
	
	
	void participer()
	{
		String email = new SessionManager(this).getUserDetails().getEmail();
		
		Uri uri = Uri.parse( C.site_url("mobile/sondages_public/index/"+email) );
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);

	}
	private void initActionBarButtons(){
		 
		 logOut = (ImageButton) findViewById(R.id.ab_right_button);
		 homePage = (ImageButton) findViewById(R.id.ab_left_button);
		 
		 logOut.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new SessionManager(getApplicationContext()).logoutUser();
				
			}
		});
		 homePage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				finish();
				
			}
		});
		 
	}
}
